package cn.lisens.thymeleaf.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.lisens.thymeleaf.entity.Product;
import cn.lisens.thymeleaf.entity.User;

@Controller
@RequestMapping("")
public class HomeController {
	
	private List<User> users = new ArrayList<>();

	@RequestMapping("/")
	public String index(Model model) {
		model.addAttribute("message", "你好");
		model.addAttribute("users", users);
        return "index";
	}
	
	@RequestMapping("/userForm")
	public String form(User user,Model model) {
		model.addAttribute("user", user);
        return "userForm";
	}
	
	@RequestMapping("/save")
	public String save(User user,Model model) {
		users.add(user);
        return "redirect:/";
	}
	
	@RequestMapping("/home")
	public String home(Model model) {
		model.addAttribute("title", "产品首页");
		List<Product> items = new ArrayList<Product>();
		Product item = new Product();
		item.setTitle("雪糕");
		item.setDescription("好吃的雪糕");
		items.add(item);
		Product item2 = new Product();
		item2.setTitle("鸭梨");
		item2.setDescription("不好吃你别买");
		items.add(item2);
		model.addAttribute("items", items);
        return "home";
	}
	
	@RequestMapping("/example")
	public String task(Model model) {
		model.addAttribute("title", "产品首页");
		List<Product> items = new ArrayList<Product>();
		Product item = new Product();
		item.setTitle("雪糕");
		item.setDescription("好吃的雪糕");
		items.add(item);
		model.addAttribute("task", item);
        return "example";
	}
	
	@RequestMapping("/mobile")
	public String mobile(Model model) {
		model.addAttribute("title", "产品首页");
		List<Product> items = new ArrayList<Product>();
		Product item = new Product();
		item.setTitle("雪糕");
		item.setDescription("好吃的雪糕");
		items.add(item);
		Product item2 = new Product();
		item2.setTitle("鸭梨");
		item2.setDescription("不好吃你别买");
		items.add(item2);
		model.addAttribute("products", items);
        return "mobile/index";
	}
	
	@RequestMapping("/login")
	public String login(Model model) {
		model.addAttribute("mobile", "");
		model.addAttribute("code", "");
        return "mobile/login";
	}
}
